package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @version v1.0
 * @package com.itheima.controller
 * @description:
 * @author: Ewaits
 * @date: 2019/7/5 16:34
 */
@RestController
@RequestMapping("role")
public class RoleController {
    @Reference
    private RoleService roleService;

    @RequestMapping("findAll")
    public Result findAll() {
        try {
            List<Role> list = roleService.findAll();
            if (CollectionUtils.isEmpty(list)) {
                return new Result(false, "无角色信息");
            }
            return new Result(true, "查询角色信息成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色信息失败");
        }
    }


    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
    }

    @RequestMapping("add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            roleService.add(role, permissionIds, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加角色失败");
        }
        return new Result(true, "添加角色成功");
    }

    @RequestMapping("findRoleById")
    public Result findRoleById(Integer id) {
        try {
            Role role = roleService.findRoleById(id);
            if (ObjectUtils.isEmpty(role)) {
                return new Result(false, "查询角色失败");
            }
            return new Result(true, "查询角色成功", role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色失败");
        }
    }

    @RequestMapping("findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        try {
            Integer[] permissionIds = roleService.findPermissionIdsByRoleId(id);
            return new Result(true, "查询角色权限数据成功", permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色权限数据失败");
        }
    }

    @RequestMapping("findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id) {
        try {
            Integer[] menuIds = roleService.findMenuIdsByRoleId(id);
            return new Result(true, "查询角色菜单数据成功", menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色菜单数据成功");
        }
    }


    @RequestMapping("deleteRoleById")
    public Result deleteRoleById(Integer id) {
        try {
            roleService.deleteRoleById(id);
        } catch (RuntimeException re) {
            re.printStackTrace();
            return new Result(false, re.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除角色失败");
        }
            return new Result(true, "删除角色成功");
    }


    @RequestMapping("edit")
    public Result edit(@RequestBody Role role, Integer[] menuIds, Integer[] permissionIds) {
        try {
            roleService.edit(role, menuIds, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑角色失败");
        }
        return new Result(true, "编辑角色成功");
    }
}
